package org.jeecg.modules.boc.utils.retrofit2;


import lombok.extern.slf4j.Slf4j;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.io.IOException;

@Slf4j
public class HTTP {
    //外网测试
    //public static String BASE_URL= "https://qyapi.weixin.qq.com/cgi-bin/";
    //行信生产-中转
    //public static String BASE_URL = "http://21.148.8.247:13082/cgi-bin/";
    //行信生产
//    public static String BASE_URL = "http://imp.bank-of-china.com:13082/";//一期
//    public static String BASE_URL = "https://imp.bank-of-china.com/";//二期 通过接口文档所得
    //行信测试
    //public static String BASE_URL="http://22.148.209.8:88/cgi-bin/";


    private static Retrofit retrofit;

    private static HTTP http;

    //这是单例模式，一般用于比较大，复杂的对象，只初始化一次，应该还有一个private的构造函数，使得不能用new来实例化对象，只能调用getInstance方法来得到对象，
    // 而getInstance保证了每次调用都返回相同的对象。
    public static HTTP getInstance(String baseUrl) {
        log.info("getInstance-----baseUrl:"+baseUrl);
        if (http == null) {
            http = new HTTP(baseUrl);
            return http;
        }
        return http;
    }

    //TODO 2023年12月6日11:26:08 暂时  每次进来都重新new对象,待调试之后修改回去
//    public static HTTP getInstance(String baseUrl) {
//        log.info("getInstance-----baseUrl:"+baseUrl);
//        http = new HTTP(baseUrl);
//        return http;
//    }

    private HTTP(String baseUrl) {
        log.info("HTTP-----baseUrl:"+baseUrl);
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }


    public <T> T getRequest(Class<T> service) {
        return retrofit.create(service);
    }


    /**
     * 同步请求
     *
     * @param <B>
     */
    public <B> B synVisit(Call<B> call) {
        log.info("url:" + call.request().url() +
                "\nmethod:" + call.request().method() +
                "\nquery:" + call.request().headers() +
                "\nbody:" + call.request().body());
        B a = null;
        try {
            Response<B> response = call.execute();
            log.info("isSuccessful:" + response.isSuccessful());
            log.info("body:" + response.body());
            log.info("errorBody:" + response.errorBody());
            log.info("code:" + response.code());
            a = response.body();
        } catch (IOException e) {
            log.info(e.getMessage());
            e.printStackTrace();
        }
        return a;
    }

    /**
     * 异步请求
     *
     * @param <A>
     */
//    public <A> void asyVisit(Call<A> call, final CallBack callback) {
//
//        call.enqueue(new Callback<A>() {
//            @Override
//            public void onResponse(Call<A> call, Response<A> response) {
//                callback.success(response.body());
//            }
//
//            @Override
//            public void onFailure(Call<A> call, Throwable throwable) {
//                callback.error(throwable.getMessage());
//            }
//        });
//    }

}
